package com.inspur.gs.sbssdl.sbyxgl.wxjl.wxjl.back.bizactions;

import com.inspur.edp.bef.api.be.IBEManagerContext;
import com.inspur.edp.bef.api.be.IBEService;
import com.inspur.edp.bef.api.be.IBusinessEntity;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.changeset.ValueObjModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class sbssWxjlWriteBackStatusMgrAction extends AbstractManagerAction<String> {
    private String dataid;
    private String ryid;

    public sbssWxjlWriteBackStatusMgrAction(IBEManagerContext managerContext,
                                            String dataid, String ryid) {
        super(managerContext);
        this.dataid = dataid;
        this.ryid = ryid;
    }

    @Override
    public void execute() {
        SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
        String sbtzconfigID = "efd56b39-0e8d-4441-9fa1-2b019b6fd862";
        String repairConfigID = "36d553b0-e4f7-4aeb-bdf9-c747396ab5a0";
        try {
            ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
            IStandardLcp repairLcp = iLcpFactory.createLcpByBEId(repairConfigID);
            IEntityData repairData = repairLcp.retrieve(dataid).getData(); //故障单
            AssociationInfo sbidInfo = (AssociationInfo) repairData.getValue("Sbid");
            String sbid = sbidInfo.getValue("Sbid").toString(); //获取设备id
            String wxdh = repairData.getValue("Wxdh").toString();

            IStandardLcp sbtzLcp = iLcpFactory.createLcpByBEId(sbtzconfigID);
            IEntityData sbtz = sbtzLcp.retrieve(sbid).getData(); //获取台账信息

            //主表变更
            ModifyChangeDetail change = new ModifyChangeDetail(sbtz.getID());
            AssociationInfo ztInfo = new AssociationInfo();
            ztInfo.setValue("zt_ID", "9a453168-96d9-4389-9654-a7930a7f4481");
            change.getPropertyChanges().put("zt", ztInfo);  //将使用状态改为在用

            //子表变更
            ValueObjModifyChangeDetail bemodify = new ValueObjModifyChangeDetail();
            bemodify.setItem("updValue", "在用");
            bemodify.setItem("changeUser", ryid);
            bemodify.setItem("valueFrom", "维修记录" + wxdh);
            sbtzLcp.modifyVariable(bemodify);

            sbtzLcp.modify(change);
            sbtzLcp.save();

        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
        }

    }

    private IBusinessEntity getEntity(String dataId) {
        return (IBusinessEntity) super.getBEManagerContext().getEntity(dataId);
    }

    private IBEService getMgr() {
        return (IBEService) super.getBEManagerContext().getBEManager();
    }
}